package com.qw.fmmall.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qw.fmmall.vo.ResStatus;
import com.qw.fmmall.vo.ResultVO;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class CheckTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //放行option请求
        String method = request.getMethod();
        if ("OPTIONS".equalsIgnoreCase(method)){
            return true;
        }

        String token = request.getHeader("token");
        System.out.println("----------"+token);
        if (token == null){
            ResultVO resultVO = new ResultVO(ResStatus.RES_STATUS2.getValue(),"请先登录",null);
            doResponse(response,resultVO);
        }else{
            try{
                //验证token
                JwtParser parser = Jwts.parser();
                parser.setSigningKey("qwqwqw");
                Jws<Claims> claimsJws = parser.parseClaimsJws(token);
                return true;
            }catch (ExpiredJwtException e){
                ResultVO resultVO = new ResultVO(ResStatus.RES_STATUS2.getValue(),"登录过期，请重新登录！",null);
                doResponse(response,resultVO);
            }catch (UnsupportedJwtException e){
                ResultVO resultVO = new ResultVO(ResStatus.RES_STATUS2.getValue(),"Token不合法！",null);
                doResponse(response,resultVO);
            }catch (Exception e){
                ResultVO resultVO = new ResultVO(ResStatus.RES_STATUS2.getValue(),"请先登录！",null);
                doResponse(response,resultVO);
            }

        }
        return false;
    }

    private void doResponse(HttpServletResponse response,ResultVO resultVO) throws IOException{
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String s = new ObjectMapper().writeValueAsString(resultVO);
        out.print(s);
        out.flush();
        out.close();
    }

}
